package com.ikun.yummy.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ikun.yummy.R;
import com.ikun.yummy.bean.FollowInfo;
import com.ikun.yummy.bean.UserInfo;
import com.ikun.yummy.manager.FollowOPManager;
import com.ikun.yummy.manager.UserOpManager;

import java.io.File;
import java.util.List;
public class FollowListAdapter extends ArrayAdapter<FollowInfo> {
    private final int resourceId;
    private final UserOpManager userOpManager;
    private final OnFollowClickListener click;
    private FollowOPManager followOPManager;

    public FollowListAdapter(Context context, int textViewResourceId, List<FollowInfo> objects,OnFollowClickListener click){
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
        userOpManager=new UserOpManager(context);
        this.click=click;
        followOPManager=new FollowOPManager(context);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        FollowInfo item=getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView==null){
            view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder= new ViewHolder();
            viewHolder.username=view.findViewById(R.id.item_follow_username);
            viewHolder.img=view.findViewById(R.id.item_follow_img);
            viewHolder.button=view.findViewById(R.id.item_follow_unfollow);
            view.setTag(viewHolder);
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }
        UserInfo userInfo;
        if (item.type==1){
            userInfo = userOpManager.queryUser(item.othername);
            viewHolder.button.setText("取关");
        }else{
            userInfo=userOpManager.queryUser(item.othername);
            //再多判断一个已经回关
            FollowInfo followInfo = followOPManager.queryFollowerSingle(item.username, item.othername);
            if (followInfo!=null){
                viewHolder.button.setText("互相关注");
            }else {
                viewHolder.button.setText("回关");
            }
        }
        File file=new File(userInfo.userimage);
        if (file.exists()){
            viewHolder.img.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
        }else {
            viewHolder.img.setImageResource(R.drawable.app_logo);
        }
        viewHolder.username.setText(userInfo.username);
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click.buttonClick(v,item);
            }
        });
        return view;
    }

    static class ViewHolder{
        ImageView img;
        TextView username;
        Button button;
    }
}